package meb

import (
	"github.com/gin-gonic/gin"
	"github.com/mosongcc/gosdk/weixin_oauth"
	"net/http"
)

// WeixinAuthorize 微信网页授权跳转
func (ctl *Controller) WeixinAuthorize(ctx *gin.Context) (err error) {
	var (
		domain = ctx.Request.Host
		state  = ctx.Query("state")
		scope  = ctx.Query("scope")
		wechat = ctl.ConfigService.Wechat()
		appid  = wechat.Appid
		// 回调拼接 query参数 code 与 state  示例 https://wechat-oauth2.mosong.cc/vptst.mosong.cc/vpapi/meb/weixin-oauth-callback?code=041cEU000N1iLS1pOP2003LhO72cEU0K&state=xxx
		redirectUri = wechat.CbBaseUrl + domain + PATH_WEIXIN_OAUTH2_CALLBACK
	)
	if scope != weixin_oauth.SnsapiUserinfo {
		scope = weixin_oauth.SnsapiBase
	}
	ctx.Redirect(http.StatusFound, weixin_oauth.Authorize(appid, redirectUri, scope, state))
	return
}
